package com.easymin.weixin.manager.impl;

import com.easymin.weixin.common.DataGrid;
import com.easymin.weixin.entity.ApiLogs;
import com.easymin.weixin.manager.ApiLogsManager;
import com.easymin.weixin.service.ApiLogsService;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DefaultApiLogsManager implements ApiLogsManager {

  @Autowired
  private ApiLogsService apiLogsService;

  @Override
  public DataGrid<ApiLogs> findAll(int page, int rows) {

    int offset = (page - 1) * rows;
    RowBounds rowBounds = new RowBounds(offset, rows);

    List<ApiLogs> apiLogses = apiLogsService.findAll(rowBounds);

    long total = apiLogsService.count();

    DataGrid<ApiLogs> dataGrid = new DataGrid<>();
    dataGrid.setRows(apiLogses);
    dataGrid.setTotal(total);

    return dataGrid;
  }

  @Override
  public int insert(ApiLogs apiLogs) {

    apiLogs.setCreated(new Date());

    return apiLogsService.insert(apiLogs);
  }
}
